package controller

import (
	"device-admin/internal/app/web/service/device"
	"device-admin/internal/app/web/service/location"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"strconv"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Robot struct{}

// GetNavHome 导航首页数据
func (r Robot) GetNavHome(c *gin.Context) {
	robotID := c.Query("robot_id")
	// 获取设备详情
	deviceInfo, _ := device.DeviceDetail("1", "", robotID)
	locationName := deviceInfo.LocationName
	// 获取位置详情
	locationDetail := location.LocationDetail(deviceInfo.LocationID)
	// 获取导航详情
	currentFloor, err := location.Robot{}.GetNavMap(robotID)
	if err != nil {
		response.JSON(c, nil, err)
		return
	}
	// 获取商场楼层列表
	floors, err := location.Robot{}.GetLocationFloorList(robotID)
	if err != nil {
		response.JSON(c, nil, err)
		return
	}
	// 获取推荐的品牌列表
	brands, err := location.Robot{}.GetRecommendedBrandList(robotID)
	if err != nil {
		response.JSON(c, nil, err)
	}
	response.JSON(c, map[string]interface{}{
		"location_name":    locationName,          // 位置名称
		"nav_url":          locationDetail.NavURL, // 导航链接
		"nav_map":          currentFloor.Map,      // 导航地图url地址
		"current_floor_id": currentFloor.ID,       // 设备当前所在的楼层id
		"floor_list":       floors,                // 设备所在商场的楼层列表
		"brand_list":       brands,                // 设备所在商场的品牌列表
	}, nil)
}

// GetNavMap 获取导航地图
func (Robot) GetNavMap(c *gin.Context) {
	floor, err := location.Robot{}.GetNavMap(c.Query("robot_id"))
	response.JSON(c, map[string]interface{}{
		"floor_id": floor.ID,
		"map_url":  floor.Map,
	}, err)
}

// GetLocationFloorList 获取楼层
func (Robot) GetLocationFloorList(c *gin.Context) {
	floors, err := location.Robot{}.GetLocationFloorList(c.Query("robot_id"))
	response.JSON(c, floors, err)
}

// GetNavPositionList 获取位置点
func (Robot) GetNavPositionList(c *gin.Context) {
	positions, err := location.Robot{}.GetNavPositionList(c.Query("robot_id"))
	response.JSON(c, positions, err)
}

// GetFloorBrandList 获取楼层品牌
func (Robot) GetFloorBrandList(c *gin.Context) {
	robotID := c.Query("robot_id")
	locationID := c.Query("location_id")
	floorID := c.Query("floor_id")
	brands, err := location.Robot{}.GetFloorBrandList(robotID, locationID, floorID)
	response.JSON(c, brands, err)
}

// GetRecommendedBrandList 获取推荐品牌
func (Robot) GetRecommendedBrandList(c *gin.Context) {
	brands, err := location.Robot{}.GetRecommendedBrandList(c.Query("robot_id"))
	response.JSON(c, brands, err)
}

// GetSearchRecommendedBrandList 获取搜索推荐
func (Robot) GetSearchRecommendedBrandList(c *gin.Context) {
	brands, err := location.Robot{}.GetSearchRecommendedBrandList(c.Query("robot_id"))
	response.JSON(c, brands, err)
}

// GetShortestPath 获取最短路径
func (Robot) GetShortestPath(c *gin.Context) {
	robotID := c.Query("robot_id")
	dstPositionID := c.Query("dst_position_id")
	dstFloorID := c.Query("dst_floor_id")
	dstPositionDesc := c.Query("dst_position_desc")
	pathDetail, err := location.Robot{}.GetShortestPath(robotID, dstPositionID, dstFloorID, dstPositionDesc)
	response.JSON(c, pathDetail, err)
}

// 获取导航链接
func (Robot) GetNavURL(c *gin.Context) {
	// GetNavURL
	url, err := location.GetNavURL(c.Request.Context(), c.Query("robot_id"), c.Query("dst_shop_no"))
	// 返回
	response.JSON(c, map[string]interface{}{
		"url": url,
	}, err)
}

type RobotMallService struct{}

// List 商场服务列表
func (RobotMallService) List(c *gin.Context) {
	records := location.MallService{}.List(
		c.Request.Context(),
		c.Query("robot_id"),
		c.Query("location_id"),
		1, 1,
	)
	response.JSON(c, map[string]interface{}{
		"list":  records,
		"total": len(records),
	}, nil)
}

// Detail 商场服务详情
func (RobotMallService) Detail(c *gin.Context) {
	res := location.MallService{}.Detail(c.Request.Context(), c.Query("service_id"))
	response.JSON(c, res, nil)
}

// HostKeywords 商场服务热词
func (RobotMallService) HotKeywords(c *gin.Context) {
	detail, _ := dao.Device{}.Detail(c.Request.Context(), dal.Q, c.Query("robot_id"))
	if detail == nil {
		response.JSON(c, []string{}, errorx.New("无效的设备", -1))
		return
	}
	keywords, _ := dao.MallServiceQAKeywords{}.HotKeywords(c.Request.Context(), dal.Q, detail.LocationID)
	if len(keywords) == 0 {
		keywords = []string{}
	}
	response.JSON(c, gin.H{
		"keywords": keywords,
	}, nil)
}

// QAList 问题查询
func (RobotMallService) QASearch(c *gin.Context) {
	detail, _ := dao.Device{}.Detail(c.Request.Context(), dal.Q, c.Query("robot_id"))
	if detail == nil {
		response.JSON(c, []string{}, errorx.New("无效的设备", -1))
		return
	}
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	if limit >= 10 {
		limit = 10
	}
	list, total, _ := dao.MallServiceQA{}.Search(c.Request.Context(), dal.Q, detail.LocationID, c.Query("question"), page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}
